﻿using KinonekoSoftware.Studio.Models.Theories;

namespace KinonekoSoftware.Studio.Pages.Instances.Geography
{
    public class TheoryRankEditorViewModel : RepositoryGalleryItem
    {
        private readonly TheoryEngine_Rarity  _theory_rarity;
        private readonly TheoryEngine_Element _theory_element;
        private readonly IRepository          _repository;
        private readonly RarityComparer       _comparer_rarity;

        private static readonly PseudoHeaderModel _pseudoHeaderModel = new PseudoHeaderModel();
        

        public TheoryRankEditorViewModel()
        {
            _repository      = Platform.GetRepository();
            _comparer_rarity = new RarityComparer();
            _theory_rarity   = Platform.GetEngine<TheoryEngine_Rarity>();
            _theory_element  = Platform.GetEngine<TheoryEngine_Element>();

            Elements = new ObservableCollection<TheoryOfElementPresenter>();
            Rarities = new ObservableCollection<TheoryOfRarityPresenter>();
        }

        private void Initialize()
        {

            Elements.AddMany(_theory_element.Collection
                                            .Select(TheoryOfElementPresenter.Select), true);
            
            Rarities.AddMany(_theory_rarity.Collection
                                           .Select(TheoryOfRarityPresenter.Select)
                                           .OrderDescending(_comparer_rarity), true);
        }

        protected override void OnStart()
        {
            Initialize();
            base.OnStart();
        }

        protected override void OnResume()
        {
            Initialize();
            base.OnResume();
        }


        public IEnumerable<object> CompositeElements
        {
            get
            {
                yield return _pseudoHeaderModel;
                
                foreach (var element in Elements)
                {
                    yield return element;
                }
            }
        }
        
        public IEnumerable<object> CompositeRarities
        {
            get
            {
                yield return _pseudoHeaderModel;
                
                foreach (var element in Rarities)
                {
                    yield return element;
                }
            }
        }
        

        public ObservableCollection<TheoryOfRarityPresenter>  Rarities { get; }
        public ObservableCollection<TheoryOfElementPresenter> Elements { get; }
    }
}